<?php
include_once('config/config.php');

// var_dump($_POST);
if ($_POST) {
  if ($_POST['action'] == 'del') {
    $id = $_POST['id'];
    $admin = findOne("select * from admin where id = '$id'");
    if (!$admin) {
      error("该管理员无法删除");
    }
    $result = delete('admin', "id = '$id'");
    if ($result > 0) {
      success("删除成功");
    } else {
      error("删除失败");
    }
  }
}

$where = 1;
// 接收地址的搜索关键字参数
$keyword = $_GET['keyword'];
if(!empty($keyword))
{
  $where = "`username` LIKE '%$keyword%'";
}
// 获取地址的当前页参数
$page = isset($_GET['page']) ? $_GET['page'] : 1;
// 获取当前表的数据总数
// $count = findOne("SELECT COUNT(*) AS num FROM `admin`");
$count = findOne("SELECT COUNT(*) AS num FROM `admin` where $where");
// 每页显示条数
$limit = 2;
// 页码
$size = ceil($count['num'] / $limit);
$ShowPage = page($page,$count['num'],$limit,$size);
$start = ($page - 1) * $limit;
// 查询的sql语句
$sql = "SELECT * FROM `admin` where $where LIMIT $start,$limit";
$dataList = findAll($sql);
$flag = 1;
if (!$dataList) {
  $flag = 0;
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
  <!-- 引入 -->
  <?php include_once('./common/meta.php') ?>
</head>
<style>
  .search {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    height: 60px;
  }
  .search input, .search button {
    margin: 0;
    outline: none;
  }
</style>
<body>

  <!-- 引入头部导航 -->
  <?php include_once('./common/navbar.php') ?>

  <!-- 引入侧边菜单栏 -->
  <?php include_once('./common/sidebar.php') ?>

  <div class="content">
    <div class="header">
      <h1 class="page-title">管理员列表</h1>
    </div>
    <ul class="breadcrumb">
      <li><a href="index.php">Home</a> <span class="divider">/</span></li>
      <li class="active">adminList</li>
    </ul>

    <div class="container-fluid">
      <div class="row-fluid">
        <!-- 添加按钮 -->
        <div class="btn-toolbar">
          <button class="btn btn-primary" onClick="location='addAdmin.php'"><i class="icon-plus"></i>添加管理员</button>
          <form method="get" class="search">
            <input type="text" name="keyword">
            <button class="btn">搜索</button>
          </form>
        </div>
        <!-- 列表 -->
        <div class="well">
          <table class="table">
            <thead>
              <tr>
                <th>ID</th>
                <th>管理员名称</th>
                <th>头像</th>
                <th>密码盐</th>
                <th style="width: 26px;"></th>
              </tr>
            </thead>
            <?php if ($flag == 1) { ?>
              <tbody>
                <?php foreach ($dataList as $item) { ?>
                  <tr>
                    <td><?php echo $item['id'] ?></td>
                    <td><?php echo $item['username'] ?></td>
                    <td><img src="<?php echo $item['avatar'] ? $item['avatar'] : '' ?>" alt="" width="50px"></td>
                    <td><?php echo $item['salt'] ?></td>
                    <td>
                      <a href="editAdmin.php?id= <?php echo $item['id']; ?> "><i class="icon-pencil"></i></a>
                      <a href="#myModal" class="del" role="button" data-id="<?php echo $item['id']; ?>" data-toggle="modal"><i class="icon-remove"></i></a>
                    </td>
                  </tr>
                <?php } ?>
              </tbody>
            <?php } ?>
            <?php if ($flag == 0) { ?>
              <tbody>
                <tr><td colspan="10" style="text-align: center;font-size: 25px;">暂无数据</td></tr>
              </tbody>
            <?php }?>
          </table>
        </div>
        <!-- 分页 -->
        <?php echo $ShowPage; ?>
        <!-- 删除提示  弹窗 -->
        <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h3 id="myModalLabel">Delete Confirmation</h3>
          </div>
          <div class="modal-body">
            <p class="error-text"><i class="icon-warning-sign modal-icon"></i>Are you sure you want to delete the user?</p>
          </div>
          <div class="modal-footer">
            <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
            <button class="btn btn-danger" data-dismiss="modal">Delete</button>
          </div>
        </div>
        <?php include_once('./common/footer.php') ?>
      </div>
    </div>
  </div>

</body>

</html>
<?php include_once('./common/script.php') ?>
<script>
  $('.del').click(function() {
    let id = $(this).data('id')

    $('.btn-danger').attr('data-id', id)
  })

  $('.btn-danger').click(function() {
    let id = $(this).data('id')
    console.log(id)
    // 发起异步请求
    $.ajax({
      // 请求方法 get post 一般都是用post
      type: 'post',
      // 请求地址
      url: 'adminList.php',
      // 请求需要的参数
      data: {
        id, //id:id
        action: 'del'
        // id:5
      },
      // 请求返回的数据类型
      dataType: 'json', // html text xml json
      // 请求成功的回调函数 http 状态码 = 200 
      success: function(res) {
        if (res.code === 1) {
          alert(res.msg)
          location.reload()
        } else {
          alert(res.msg)
        }
      },
      // 请求失败的回调函数 404 301 500等
      error: function(error) {
        console.log(error)
      }

    })

  })
</script>